package com.tarena.lbs.user.dao.repository.Impl;

import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.param.AdminLoginParam;
import com.tarena.lbs.user.pojo.param.UserLoginParam;
import com.tarena.lbs.user.pojo.vo.LoginVO;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.websocket.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
@Slf4j
public class UserGroupRepositoryImpl implements UserGroupRepository {

    @Autowired
    private UserGroupMapper userGroupMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public LoginVO login(UserLoginParam param) {
        log.debug("仓储层校验参数{}" , param);
        LoginVO loginVO = userGroupMapper.select(param);
        if (loginVO== null) {
            log.warn("手机号不存在: {}", param.getPhone());
            throw new JwtException("手机号或者密码错误"); // 应该使用统一的异常处理
        }
        if (StringUtils.isEmpty(param.getPassword())) {
            throw new JwtException("密码不能为空");
        }

        // 3. 验证密码
        if (!loginVO.getJwt().equals(param.getPassword())) {
            log.warn("密码错误: {}", param.getPassword());
            throw new JwtException("密码错误");
        }

        return loginVO;
    }



}
